package com.ruoyi.supervise.service.impl;

import java.util.List;

import javax.xml.xpath.XPathConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.domain.ConDrawing;
import com.ruoyi.supervise.domain.WDCertificateInfo;
import com.ruoyi.supervise.domain.WDCertificateText;
import com.ruoyi.supervise.domain.WDCertificateTextUnit;
import com.ruoyi.supervise.domain.WdCertiLogInnerText;
import com.ruoyi.supervise.mapper.WDCertificateInfoMapper;
import com.ruoyi.supervise.service.IWDCertificateInfoService;
import com.ruoyi.supervise.util.XmlReadUtil;

import cn.hutool.core.util.XmlUtil;

/**
 * @ClassName: MyTestServiceImpl
 * @Description: 测试从数据源
 * @Author wtj
 * @DateTime 2020年3月9日 上午10:41:31
 */
@Service
@DataSource(value = DataSourceType.SLAVE)
public class WDCertificateInfoServiceImpl implements IWDCertificateInfoService 
{
    @Autowired
    private WDCertificateInfoMapper wDCertificateInfoMapper;

	@Override
	public List<WDCertificateInfo> selectWDCertificateInfoByUnitProGuid(String unitProjectGuid) {
		// TODO Auto-generated method stub
		return wDCertificateInfoMapper.selectWDCertificateInfoByUnitProGuid(unitProjectGuid);
	}

	/**
	 * 根据单位工程标识符获取图审合格证号
	 */
	@Override
	public WDCertificateInfo selectWDCertInfoByUnitGuid(String unitProjectGuid) {
		WDCertificateInfo ci = new WDCertificateInfo();
		List<WDCertificateInfo> wdciList = wDCertificateInfoMapper.selectWDCertificateInfoByUnitProGuid(unitProjectGuid);
		if(!StringUtils.isEmpty(wdciList)) {
			String nodePath = "/PsDataPacket/UnitProjects/FDataList/WDCertiUnitProject";
			for(WDCertificateInfo wdci:wdciList) {
				if(!StringUtils.isEmpty(wdci.getCertificateText())) {
					WDCertificateTextUnit ctu = new WDCertificateTextUnit();
					ctu.setGuid("Guid");
					ctu.setName("Name");
					ctu.setLayer("Layer");
					ctu.setUpArea("UpArea");
					ctu.setDownArea("DownArea");
					ctu.setApplicationArea("ApplicationArea");
					ctu.setCollectHotArea("CollectHotArea");
					List<WDCertificateTextUnit> wdCtuList = XmlReadUtil.getCertificateTextXml(wdci.getCertificateText(), nodePath, ctu);
					if(!StringUtils.isEmpty(wdCtuList)) {
						for(WDCertificateTextUnit wdctu:wdCtuList) {
							if(wdctu.getGuid().equals(unitProjectGuid)) {
								return wdci;
							}
						}
					}
					
				}
				
			}
		}
		
		return null;
	}

	@Override
	public WDCertificateInfo selectWDCertificateInfoByGuid(String guid) {
		// TODO Auto-generated method stub
		return wDCertificateInfoMapper.selectWDCertificateInfoByGuid(guid);
	}

	@Override
	public WDCertificateText selectWDCertificateTextUnitByGuid(String guid) {
		WDCertificateInfo ci = wDCertificateInfoMapper.selectWDCertificateInfoByGuid(guid);
		WDCertificateText wdct = new WDCertificateText();
		if(!StringUtils.isEmpty(ci.getCertificateText())) {
			Document docResult=XmlUtil.readXML(ci.getCertificateText());
			Object designComany = XmlUtil.getByXPath("//PsDataPacket/DesignComany", docResult, XPathConstants.STRING);
			wdct.setDesignComany(String.valueOf(designComany));
			Object designLeadMan = XmlUtil.getByXPath("//PsDataPacket/DesignLeadMan", docResult, XPathConstants.STRING);
			wdct.setDesignLeadMan(String.valueOf(designLeadMan));
			Object examineAgency = XmlUtil.getByXPath("//PsDataPacket/ExamineAgency", docResult, XPathConstants.STRING);
			wdct.setExamineAgency(String.valueOf(examineAgency));
			Object examineAgencyLeadMan = XmlUtil.getByXPath("//PsDataPacket/ExamineAgencyLeadMan", docResult, XPathConstants.STRING);
			wdct.setExamineAgencyLeadMan(String.valueOf(examineAgencyLeadMan));
			return wdct;
		}else {
			return null;
		}
	}

	@Override
	public List<WdCertiLogInnerText> selectWdCertiLogByProReviewGuid(String prospectReviewGuid) {
		// TODO Auto-generated method stub
		return wDCertificateInfoMapper.selectWdCertiLogByProReviewGuid(prospectReviewGuid);
	}

	@Override
	public String selectWdCertiLogByGuid(String guid) {
		// TODO Auto-generated method stub
		return wDCertificateInfoMapper.selectWdCertiLogByGuid(guid);
	}

	@Override
	public WDCertificateText selectWDCertificateTextByGuid(String guid) {
		String CertificateTextStr=wDCertificateInfoMapper.selectWdCertiLogByGuid(guid);
		WDCertificateText wdct = new WDCertificateText();
		if(!StringUtils.isEmpty(CertificateTextStr)) {
			Document docResult=XmlUtil.readXML(CertificateTextStr);
			Object certificateNum = XmlUtil.getByXPath("//PsDataPacket/CertificateNum", docResult, XPathConstants.STRING);
			wdct.setCertificateNum(String.valueOf(certificateNum));
			Object examineAgency = XmlUtil.getByXPath("//PsDataPacket/ExamineAgency", docResult, XPathConstants.STRING);
			wdct.setExamineAgency(String.valueOf(examineAgency));
			Object examineDate = XmlUtil.getByXPath("//PsDataPacket/ExamineDate", docResult, XPathConstants.STRING);
			wdct.setExamineDate(String.valueOf(examineDate));
			Object chUnitProject = XmlUtil.getByXPath("//PsDataPacket/ChUnitProject", docResult, XPathConstants.STRING);
			wdct.setChUnitProject(String.valueOf(chUnitProject));
			Object buildAddress = XmlUtil.getByXPath("//PsDataPacket/BuildAddress", docResult, XPathConstants.STRING);
			wdct.setBuildAddress(String.valueOf(buildAddress));
			Object buildCompany = XmlUtil.getByXPath("//PsDataPacket/BuildCompany", docResult, XPathConstants.STRING);
			wdct.setBuildCompany(String.valueOf(buildCompany));
			Object proCompany = XmlUtil.getByXPath("//PsDataPacket/ProCompany", docResult, XPathConstants.STRING);
			wdct.setProCompany(String.valueOf(proCompany));
			Object prosCropCertNum = XmlUtil.getByXPath("//PsDataPacket/ProsCropCertNum", docResult, XPathConstants.STRING);
			wdct.setProsCropCertNum(String.valueOf(prosCropCertNum));
			Object designComany = XmlUtil.getByXPath("//PsDataPacket/DesignComany", docResult, XPathConstants.STRING);
			wdct.setDesignComany(String.valueOf(designComany));
			Object designCropCertNum = XmlUtil.getByXPath("//PsDataPacket/DesignCropCertNum", docResult, XPathConstants.STRING);
			wdct.setDesignCropCertNum(String.valueOf(designCropCertNum));
			Object projectLv = XmlUtil.getByXPath("//PsDataPacket/ProjectLv", docResult, XPathConstants.STRING);
			wdct.setProjectLv(String.valueOf(projectLv));
			Object strucForm = XmlUtil.getByXPath("//PsDataPacket/StrucForm", docResult, XPathConstants.STRING);
			wdct.setStrucForm(String.valueOf(strucForm));
			Object grProSurGrade = XmlUtil.getByXPath("//PsDataPacket/GrProSurGrade", docResult, XPathConstants.STRING);
			wdct.setGrProSurGrade(String.valueOf(grProSurGrade));
			Object upBuildArea = XmlUtil.getByXPath("//PsDataPacket/UpBuildArea", docResult, XPathConstants.STRING);
			wdct.setUpBuildArea(String.valueOf(upBuildArea));
			Object downBuildArea = XmlUtil.getByXPath("//PsDataPacket/DownBuildArea", docResult, XPathConstants.STRING);
			wdct.setDownBuildArea(String.valueOf(downBuildArea));
			Object upStoreyNum = XmlUtil.getByXPath("//PsDataPacket/UpStoreyNum", docResult, XPathConstants.STRING);
			wdct.setUpStoreyNum(String.valueOf(upStoreyNum));
			Object downStoreyNum = XmlUtil.getByXPath("//PsDataPacket/DownStoreyNum", docResult, XPathConstants.STRING);
			wdct.setDownStoreyNum(String.valueOf(downStoreyNum));
			Object greenBulid = XmlUtil.getByXPath("//PsDataPacket/GreenBulid", docResult, XPathConstants.STRING);
			wdct.setGreenBulid(String.valueOf(greenBulid));
			Object greenBulidStar = XmlUtil.getByXPath("//PsDataPacket/GreenBulidStar", docResult, XPathConstants.STRING);
			wdct.setGreenBulidStar(String.valueOf(greenBulidStar));
			Object examinePerson = XmlUtil.getByXPath("//PsDataPacket/ExaminePerson", docResult, XPathConstants.STRING);
			wdct.setExaminePerson(String.valueOf(examinePerson));
			Object examineAgencyLeadMan = XmlUtil.getByXPath("//PsDataPacket/ExamineAgencyLeadMan", docResult, XPathConstants.STRING);
			wdct.setExamineAgencyLeadMan(String.valueOf(examineAgencyLeadMan));
			Object examineCorporate = XmlUtil.getByXPath("//PsDataPacket/ExamineCorporate", docResult, XPathConstants.STRING);
			wdct.setExamineCorporate(String.valueOf(examineCorporate));
			Object reviewContent = XmlUtil.getByXPath("//PsDataPacket/ReviewContent", docResult, XPathConstants.STRING);
			wdct.setReviewContent(String.valueOf(reviewContent));
			//开始获取单位工程数据
			String nodePath = "/PsDataPacket/UnitProjects/FDataList/WDCertiUnitProject";
			WDCertificateTextUnit ctu = new WDCertificateTextUnit();
			ctu.setGuid("Guid");
			ctu.setName("Name");
			ctu.setLayer("Layer");
			ctu.setUpArea("UpArea");
			ctu.setDownArea("DownArea");
			ctu.setApplicationArea("ApplicationArea");
			ctu.setCollectHotArea("CollectHotArea");
			List<WDCertificateTextUnit> wdCtuList = XmlReadUtil.getCertificateTextXml(CertificateTextStr, nodePath, ctu);
			wdct.setUnitProjects(wdCtuList);
		}
		return wdct;
	}

	@Override
	public ConDrawing selectProjectGuidByGuid(String guid) {
		// TODO Auto-generated method stub
		return wDCertificateInfoMapper.selectProjectGuidByGuid(guid);
	}

	@Override
	public String selectWDCertificateInfoByCerfiNumber(String certificateNumber) {
		// TODO Auto-generated method stub
		return wDCertificateInfoMapper.selectWDCertificateInfoByCerfiNumber(certificateNumber);
	}

	@Override
	public WDCertificateText selectWDCertificateInfoTxtByCerfiNumber(String certificateNumber) {
		String CertificateTextStr = wDCertificateInfoMapper.selectWDCertificateInfoByCerfiNumber(certificateNumber);
		WDCertificateText wdct = new WDCertificateText();
		if(!StringUtils.isEmpty(CertificateTextStr)) {
			Document docResult=XmlUtil.readXML(CertificateTextStr);
			Object certificateNum = XmlUtil.getByXPath("//PsDataPacket/CertificateNum", docResult, XPathConstants.STRING);
			wdct.setCertificateNum(String.valueOf(certificateNum));
			Object examineAgency = XmlUtil.getByXPath("//PsDataPacket/ExamineAgency", docResult, XPathConstants.STRING);
			wdct.setExamineAgency(String.valueOf(examineAgency));
			Object examineDate = XmlUtil.getByXPath("//PsDataPacket/ExamineDate", docResult, XPathConstants.STRING);
			wdct.setExamineDate(String.valueOf(examineDate));
			Object chUnitProject = XmlUtil.getByXPath("//PsDataPacket/ChUnitProject", docResult, XPathConstants.STRING);
			wdct.setChUnitProject(String.valueOf(chUnitProject));
			Object buildAddress = XmlUtil.getByXPath("//PsDataPacket/BuildAddress", docResult, XPathConstants.STRING);
			wdct.setBuildAddress(String.valueOf(buildAddress));
			Object buildCompany = XmlUtil.getByXPath("//PsDataPacket/BuildCompany", docResult, XPathConstants.STRING);
			wdct.setBuildCompany(String.valueOf(buildCompany));
			Object proCompany = XmlUtil.getByXPath("//PsDataPacket/ProCompany", docResult, XPathConstants.STRING);
			wdct.setProCompany(String.valueOf(proCompany));
			Object prosCropCertNum = XmlUtil.getByXPath("//PsDataPacket/ProsCropCertNum", docResult, XPathConstants.STRING);
			wdct.setProsCropCertNum(String.valueOf(prosCropCertNum));
			Object designComany = XmlUtil.getByXPath("//PsDataPacket/DesignComany", docResult, XPathConstants.STRING);
			wdct.setDesignComany(String.valueOf(designComany));
			Object designCropCertNum = XmlUtil.getByXPath("//PsDataPacket/DesignCropCertNum", docResult, XPathConstants.STRING);
			wdct.setDesignCropCertNum(String.valueOf(designCropCertNum));
			Object projectLv = XmlUtil.getByXPath("//PsDataPacket/ProjectLv", docResult, XPathConstants.STRING);
			wdct.setProjectLv(String.valueOf(projectLv));
			Object strucForm = XmlUtil.getByXPath("//PsDataPacket/StrucForm", docResult, XPathConstants.STRING);
			wdct.setStrucForm(String.valueOf(strucForm));
			Object grProSurGrade = XmlUtil.getByXPath("//PsDataPacket/GrProSurGrade", docResult, XPathConstants.STRING);
			wdct.setGrProSurGrade(String.valueOf(grProSurGrade));
			Object upBuildArea = XmlUtil.getByXPath("//PsDataPacket/UpBuildArea", docResult, XPathConstants.STRING);
			wdct.setUpBuildArea(String.valueOf(upBuildArea));
			Object downBuildArea = XmlUtil.getByXPath("//PsDataPacket/DownBuildArea", docResult, XPathConstants.STRING);
			wdct.setDownBuildArea(String.valueOf(downBuildArea));
			Object upStoreyNum = XmlUtil.getByXPath("//PsDataPacket/UpStoreyNum", docResult, XPathConstants.STRING);
			wdct.setUpStoreyNum(String.valueOf(upStoreyNum));
			Object downStoreyNum = XmlUtil.getByXPath("//PsDataPacket/DownStoreyNum", docResult, XPathConstants.STRING);
			wdct.setDownStoreyNum(String.valueOf(downStoreyNum));
			Object greenBulid = XmlUtil.getByXPath("//PsDataPacket/GreenBulid", docResult, XPathConstants.STRING);
			wdct.setGreenBulid(String.valueOf(greenBulid));
			Object greenBulidStar = XmlUtil.getByXPath("//PsDataPacket/GreenBulidStar", docResult, XPathConstants.STRING);
			wdct.setGreenBulidStar(String.valueOf(greenBulidStar));
			Object examinePerson = XmlUtil.getByXPath("//PsDataPacket/ExaminePerson", docResult, XPathConstants.STRING);
			wdct.setExaminePerson(String.valueOf(examinePerson));
			Object examineAgencyLeadMan = XmlUtil.getByXPath("//PsDataPacket/ExamineAgencyLeadMan", docResult, XPathConstants.STRING);
			wdct.setExamineAgencyLeadMan(String.valueOf(examineAgencyLeadMan));
			Object examineCorporate = XmlUtil.getByXPath("//PsDataPacket/ExamineCorporate", docResult, XPathConstants.STRING);
			wdct.setExamineCorporate(String.valueOf(examineCorporate));
			Object reviewContent = XmlUtil.getByXPath("//PsDataPacket/ReviewContent", docResult, XPathConstants.STRING);
			wdct.setReviewContent(String.valueOf(reviewContent));
			//开始获取单位工程数据
			String nodePath = "/PsDataPacket/UnitProjects/FDataList/WDCertiUnitProject";
			WDCertificateTextUnit ctu = new WDCertificateTextUnit();
			ctu.setGuid("Guid");
			ctu.setName("Name");
			ctu.setLayer("Layer");
			ctu.setUpArea("UpArea");
			ctu.setDownArea("DownArea");
			ctu.setApplicationArea("ApplicationArea");
			ctu.setCollectHotArea("CollectHotArea");
			List<WDCertificateTextUnit> wdCtuList = XmlReadUtil.getCertificateTextXml(CertificateTextStr, nodePath, ctu);
			wdct.setUnitProjects(wdCtuList);
		}
		return wdct;
	}

	@Override
	public ConDrawing selectProjectGuidByCerfiNumber(String certificateNumber) {
		// TODO Auto-generated method stub
		return wDCertificateInfoMapper.selectProjectGuidByCerfiNumber(certificateNumber);
	}
}
